﻿Option Explicit On
Imports System.Diagnostics.Process
Imports ArcLine
Imports ArcLine.ObjectClass

<Serializable()> Public Class CAD_Class
    Public Section_Pts As List(Of TunnelSectionPoints)
    Public angels(5) As Double
    Dim arc_lists As List(Of ArcLine.ObjectClass.ArcLine)
    Public center As TSPCoor
    Public F_Points As TunnelSectionPoints '用来储存接缝处的坐标,一共有4组

    Public Sub New()

        ' Acad = CreateObject("AutoCAD.Application")
        'Acad = New AutoCAD.AcadApplication()
        'Acad.WindowState = AcWindowState.acMin
        'Acad.Visible = False
    End Sub
    Public Sub config(ByVal section As tunnelSection)
        If Section_Pts.Count > 0 Then
            arc_lists = New List(Of ArcLine.ObjectClass.ArcLine)
            For i As Integer = 0 To Section_Pts.Count - 1
                Dim ct(2) As Double
                ct(0) = Section_Pts(i).center.Coor_H
                ct(1) = Section_Pts(i).center.Coor_V
                Dim arc As ArcLine.ObjectClass.ArcLine = New ArcLine.ObjectClass.ArcLine()
                arc.Center = New Coor(ct(0), ct(1))
                arc.Raduis = Section_Pts(i).Radius
                arc_lists.Add(arc)
                'For j As Integer = 0 To Section_Pts(i).Points.Count - 1
                '    Section_Pts(i).Points(j).Hv = Section_Pts(i).Points(j).AzimFrom(center)
                'Next
            Next
            For i As Integer = 0 To section.F_Section.Count - 1

                angels(i) = section.F_Section(i).AzimFrom(center)
            Next

            '定义最终结果的起始角度
            ' angels(0) = 45 / 180 * Math.PI
            angels(5) = 317.5 / 180 * Math.PI
        End If
    End Sub

    Public Function OutPut_Result(ByVal startA As Double, ByVal endA As Double, ByVal stepA As Double) As TunnelSectionPoints
        angels(0) = startA / 180 * Math.PI
        angels(5) = endA / 180 * Math.PI
        Dim pts As TunnelSectionPoints = New TunnelSectionPoints()
        F_Points = New TunnelSectionPoints()
        pts.center = center
        Dim jd As Double = startA / 180 * Math.PI
        Dim sp(2) As Double
        Dim iner As Coor
        sp(0) = center.Coor_H
        sp(1) = center.Coor_V
        sp(2) = 0
        Dim count As Integer = 1
        For i As Integer = 0 To 4
            Do While jd <= angels(i + 1) + 0.001
                Dim ep(2) As Double

                ep(0) = sp(0) + 100 * Math.Cos(jd - Math.PI / 2) : ep(1) = sp(1) + 100 * Math.Sin(jd - Math.PI / 2) : ep(2) = 0
                Dim pl As SectionLine = New SectionLine(New SurveyPoint(sp(0), sp(1)), New SurveyPoint(ep(0), ep(1)))

                iner = ArcLine.ObjectClass.Intersect.IntersectwithSt(arc_lists(i), pl)

                Dim pt As New SectionPoint(count.ToString, jd, "1")
                If iner Is Nothing Then
                    pt.Coor = New TSPCoor(0, 0)
                Else
                    pt.Coor = New TSPCoor(iner.Y, iner.X)
                End If

                pts.Points.Add(pt)
                count += 1
                jd += stepA / 180 * Math.PI '以某个间隔来取值
            Loop
            Dim jdtemp As Double
            jdtemp = jd
            jd = angels(i + 1)
            Dim e(2) As Double
            e(0) = sp(0) + 100 * Math.Cos(jd - Math.PI / 2) : e(1) = sp(1) + 100 * Math.Sin(jd - Math.PI / 2) : e(2) = 0
            Dim F_line As SectionLine = New SectionLine(New SurveyPoint(sp(0), sp(1)), New SurveyPoint(e(0), e(1)))
            iner = ArcLine.ObjectClass.Intersect.IntersectwithSt(arc_lists(i), F_line)
            Dim pppp As New SectionPoint(i.ToString(), 0, 0)
            If iner Is Nothing Then
                pppp.Coor = New TSPCoor(0, 0)
            Else
                pppp.Coor = New TSPCoor(iner.Y, iner.X)
            End If
            F_Points.Points.Add(pppp)
            If i = 0 Then
                Dim jd1 = angels(0)
                e(0) = sp(0) + 100 * Math.Cos(jd1 - Math.PI / 2) : e(1) = sp(1) + 100 * Math.Sin(jd1 - Math.PI / 2) : e(2) = 0
                F_line = New SectionLine(New SurveyPoint(sp(0), sp(1)), New SurveyPoint(e(0), e(1)))
                iner = ArcLine.ObjectClass.Intersect.IntersectwithSt(arc_lists(i), F_line)
                pppp = New SectionPoint(i.ToString(), 0, 0)
                pppp.Coor = New TSPCoor(iner.Y, iner.X)
                F_Points.Points.Add(pppp)
            End If
            jd = jdtemp
        Next
        pts.Call_Arc_Err()
        Return pts

    End Function

    Public Function OutPut_Result() As TunnelSectionPoints
        Return Me.OutPut_Result(42.5, 317.5, 5)
    End Function


    '计算接缝张开量用的
    Public Function Out_caculation(ByVal F_Section As List(Of SectionPoint)) As Double()
        Dim sp(2) As Double
        sp(0) = center.Coor_H
        sp(1) = center.Coor_V
        sp(2) = 0
        Dim re(3) As Double
        For i As Integer = 1 To F_Section.Count - 2
            Dim ep(2) As Double
            Dim jd As Double = F_Section(i).AzimFrom(center)
            ep(0) = sp(0) + 100 * Math.Cos(jd - Math.PI / 2) : ep(1) = sp(0) + 100 * Math.Sin(jd - Math.PI / 2) : ep(2) = 0

            Dim pl As SectionLine = New SectionLine(New SurveyPoint(sp(0), sp(1)), New SurveyPoint(ep(0), ep(1)))
            Dim intersect1(2) As Double
            Dim intersect2(2) As Double
            Dim inter1, inter2 As Coor
            inter1 = ArcLine.ObjectClass.Intersect.IntersectwithSt(arc_lists(i - 1), pl)
            inter2 = ArcLine.ObjectClass.Intersect.IntersectwithSt(arc_lists(i), pl)
            intersect1(0) = inter1.X
            intersect1(1) = inter1.Y
            intersect1(0) = inter2.X
            intersect1(1) = inter2.Y
            Dim vector As New SvVector(New TSPCoor(intersect1(1), intersect1(0)), New TSPCoor(intersect2(1), intersect2(0)))

            If jd < Math.PI Then
                If (Math.Abs(jd - vector.Azimuth) < Math.PI / 2) Then
                    re(i - 1) = vector.Distance * 1000
                Else
                    re(i - 1) = -vector.Distance * 1000
                End If
            Else
                If (Math.Abs(jd - vector.Azimuth) < Math.PI / 2) Then
                    re(i - 1) = -vector.Distance * 1000
                Else
                    re(i - 1) = vector.Distance * 1000
                End If
            End If
        Next
        Return re

    End Function

End Class
